package com.gsc.demo.controler;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gsc.demo.model.User;
import com.gsc.demo.service.UserRedisService;
import com.gsc.demo.service.UserServiceImp;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.IOException;
import java.util.List;

/**
 * file description
 * Created by 72038967 on 2019/8/12.
 */

@Controller
public class StudentControl {
    @Autowired
    private UserServiceImp mUserServiceImp;

    @Autowired
    private RedisTemplate mRedisTemplate;

    private Logger mLogger = LoggerFactory.getLogger(this.getClass().getSimpleName());
    @Autowired
    private UserRedisService mUserRedisService;

    private ObjectMapper mObjectMapper = new ObjectMapper();

    @RequestMapping(path = "/test")
    @ResponseBody
    public ResponseEntity getAll() {

        List<User> users = null;
        System.out.println("--进入控制器--");

//        mRedisTemplate.opsForValue().set("ssh","sshvalues");
//        mUserRedisService.saveObjcetByKey("test","nimade");
//       String value= mUserRedisService.getValueByKey("test");
//        System.out.println("--取--"+value);
        System.out.println("aa" + mUserRedisService.getValueByKey("json"));
        try {
            TypeReference<List<User>> regionRef = new TypeReference<List<User>>() {
            };
            if (mUserRedisService.getValueByKey("json") != null) {
                List<User> userList = mObjectMapper.readValue(mUserRedisService.getValueByKey("json"), regionRef);
                System.out.println("---" + userList);
                if (userList.size() > 0 && userList != null) {
                    users = userList;

                    System.out.println("--从缓存获取--" + users.toString());
                }
            } else {


//            mRedisTemplate.opsForList().rightPush("list_temp", users);
//            mUserRedisService.saveList("list_temp",users);
                try {

                    users = mUserServiceImp.selectAll();
                    ObjectMapper mapper = new ObjectMapper();
                   boolean isUser= mUserRedisService.saveObjcetByKey("json", mapper.writeValueAsString(users));
                    System.out.println("---"+isUser);
                } catch (JsonProcessingException e) {
                    e.printStackTrace();
                    System.out.println("json 转换异常：" + e.getMessage());
                }
                System.out.println("--从数据库获取--" + users.toString());


            }


        } catch (IOException e) {
            e.printStackTrace();
        }


        return ResponseEntity.ok(users);
    }
}
